get delete options added, clarify purpose and event description

Judy Ngai 9 years ago
parent
commit
c5b8707d88
1 changed files with 113 additions and 25 deletions
  1. 113 25
      app/models/agents/aftership_agent.rb

+ 113 - 25
app/models/agents/aftership_agent.rb

@@ -8,46 +8,134 @@ module Agents
8 8
     description <<-MD
9 9
       The Aftership agent allows you to track your shipment from aftership and emit them into events.
10 10
 
11
-      To be able to use the Aftership API, you need to generate an `API Key`. You do need a paying plan to use their tracking feature.
11
+      To be able to use the Aftership API, you need to generate an `API Key`. You need a paying plan to use their tracking feature.
12 12
 
13
-      You need a key value pair to retrieve data. The key are `get_url` and `delete_url`.
13
+      You can use this agent to either retrieve or delete data. The keys are `get` and `delete`. You have to provide a specific request and its associated option.
14
+ 
15
+      To get all trackings for your packages please enter `get` for key and `/trackings` for the option.
16
+      To get tracking for a specific tracking number, add the extra options `slug`, `tracking_number` and set `single_tracking_request` to true.
14 17
 
15
-      The options are `/trackings/export` to get tracking results for backup purposes, `/trackings/slug/tracking_number` to get tracking 
16
-      for a single tracking number, `/last_checkpoint/:slug/:tracking_number` for last checkpoint of a single tracking and `/trackings` to get all of your trackings. 
17
-      You have two options to get courier information, `/couriers` 
18
+      To get all tracking results for backup purpose set key to `get` and option to `/trackings/export`.
19
+
20
+      To get the last checkpoint of a package set key to `get` and option to `/last_checkpoint` plus provide `slug` and `tracking_number`
21
+
22
+      `slug` is a unique courier code. 
23
+      
24
+      You have two options to get courier information along with `get`, `/couriers` 
18 25
       which returns the couriers that are activiated at your account and the other is `/couriers/all` which returns all couriers.
19
-      `slug` is a unique courier code which you can get from using this agent.
20 26
 
21
-      If specified most url must be properly formatted with a `/` in front.
27
+      The `delete` option allows you to delete a specific shipment. It is `/trackings/:slug/:tracking_number`.
22 28
 
23
-      The delete option allows you to delete a specific shipment. You must provide `slug` and `tracking number`.
29
+      All urls must be properly formatted with a `/` in front.
24 30
 
25 31
       Required Options:
26 32
 
27 33
       * `Content-Type` application/json
28 34
       * `api_key` - YOUR_API_KEY.
29
-      * `key value pair request`
35
+      * `get/delete and its associated options`
30 36
     MD
31 37
 
32 38
     event_description <<-MD
33
-      It depends what kind of event that you are working on:
34
-      MD
39
+      A typical tracking event has 3 objects (attributes, tracking, and checkpoint) and it looks like this
40
+          {
41
+        "meta": {
42
+            "code": 200
43
+        },
44
+        "data": {
45
+            "page": 1,
46
+            "limit": 100,
47
+            "count": 3,
48
+            "keyword": "",
49
+            "slug": "",
50
+            "origin": [],
51
+            "destination": [],
52
+            "tag": "",
53
+            "fields": "",
54
+            "created_at_min": "2014-03-27T07:36:14+00:00",
55
+            "created_at_max": "2014-06-25T07:36:14+00:00",
56
+            "trackings": [
57
+                {
58
+                    "id": "53aa7b5c415a670000000021",
59
+                    "created_at": "2014-06-25T07:33:48+00:00",
60
+                    "updated_at": "2014-06-25T07:33:55+00:00",
61
+                    "tracking_number": "123456789",
62
+                    "tracking_account_number": null,
63
+                    "tracking_postal_code": null,
64
+                    "tracking_ship_date": null,
65
+                    "slug": "dhl",
66
+                    "active": false,
67
+                    "custom_fields": {
68
+                        "product_price": "USD19.99",
69
+                        "product_name": "iPhone Case"
70
+                    },
71
+                    "customer_name": null,
72
+                    "destination_country_iso3": null,
73
+                    "emails": [
74
+                        "email@yourdomain.com",
75
+                        "another_email@yourdomain.com"
76
+                    ],
77
+                    "expected_delivery": null,
78
+                    "note": null,
79
+                    "order_id": "ID 1234",
80
+                    "order_id_path": "http://www.aftership.com/order_id=1234",
81
+                    "origin_country_iso3": null,
82
+                    "shipment_package_count": 0,
83
+                    "shipment_type": null,
84
+                    "signed_by": "raul",
85
+                    "smses": [],
86
+                    "source": "api",
87
+                    "tag": "Delivered",
88
+                    "title": "Title Name",
89
+                    "tracked_count": 1,
90
+                    "unique_token": "xy_fej9Llg",
91
+                    "checkpoints": [
92
+                        {
93
+                            "slug": "dhl",
94
+                            "city": null,
95
+                            "created_at": "2014-06-25T07:33:53+00:00",
96
+                            "country_name": "VALENCIA - SPAIN",
97
+                            "message": "Awaiting collection by recipient as requested",
98
+                            "country_iso3": null,
99
+                            "tag": "InTransit",
100
+                            "checkpoint_time": "2014-05-12T12:02:00",
101
+                            "coordinates": [],
102
+                            "state": null,
103
+                            "zip": null
104
+                       },
105
+                        ...
106
+                    ]
107
+                },
108
+                ...
109
+            ]
110
+        }
111
+     }
112
+    MD
35 113
 
36 114
     def default_options
37 115
       { 'api_key' => 'YOUR_API_KEY',
38 116
         'Content_Type' => 'application/json',
39
-        'delete_url' => '/trackings',
40
-        'slug' => '/usps',
41
-        'tracking_number' => ''
117
+        'get' => '/trackings'
42 118
       }
43 119
     end
44 120
 
121
+    def single_tracking_request?
122
+      boolify(interpolated[:single_tracking_request]) != "false"
123
+    end
124
+
125
+    def last_checkpoint?
126
+      boolify(interpolated[:last_checkpoint]) != "false"
127
+    end
128
+
45 129
     def uri
46 130
       uri = URI.parse API_URL
47
-      if options['get_url']
48
-        uri.query = interpolated['get_url'] if uri.query.nil?
49
-      elsif options['delete_url']
50
-        uri.query = interpolated['delete_url'] + interpolated['slug'] + '/' + interpolated['tracking_number'] if uri.query.nil?
131
+      if options['get'] && single_tracking_request?
132
+        uri.query = interpolated['get']+ '/' + interpolated['slug'] + '/' + interpolated['tracking_number'] if uri.query.nil? 
133
+      elsif options['get'] && last_checkpoint?
134
+        uri.query = interpolated['get'] + '/' + interpolated['slug'] + '/' + interpolated['tracking_number'] if uri.query.nil?
135
+      elsif options['delete']
136
+        uri.query = interpolated['delete'] + interpolated['slug'] + '/' + interpolated['tracking_number'] if uri.query.nil?
137
+      else
138
+        uri.query = interpolated['get'] if uri.query.nil? 
51 139
       end
52 140
       uri.to_s.gsub('?','') 
53 141
     end
@@ -59,7 +147,7 @@ module Agents
59 147
     def validate_options
60 148
       errors.add(:base, "You need to specify a api key") unless options['api_key'].present?
61 149
       errors.add(:base, "Content-Type must be set to application/json") unless options['Content_Type'].present? && options['Content_Type'] == 'application/json'
62
-      #errors.add(:base, "You need to specify a certain request") unless options['get_url'].present? && options['delete_url'].present?
150
+      errors.add(:base, "You need to specify a certain request") unless options['get'].present? || options['delete'].present?
63 151
     end
64 152
 
65 153
     def request_options
@@ -67,12 +155,12 @@ module Agents
67 155
     end
68 156
 
69 157
     def check
70
-      response = HTTParty.get(uri, request_options)
71
-      events = JSON.parse response.body
72
-      create_event :payload => events
73
-      if options['delete_url']
74
-        delete = HTTParty.delete(uri, request_options)
75
-        #create_event :payload => delete
158
+      if options['get'] 
159
+        response = HTTParty.get(uri, request_options)
160
+        events = JSON.parse response.body
161
+        create_event :payload => events
162
+      elsif options['delete']
163
+        response = HTTParty.delete(uri, request_options)
76 164
       end
77 165
     end
78 166
   end